#include	"Material.h"
#include	<Textures/ImageTGA.h>
#include	<windows.h>
#include	<gl/gl.h>

Material::Material()
	:	m_shininess(100.0f)
	,	m_diffuse(0.8f, 0.8f, 0.8f)
	,	m_ambient(0.2f, 0.2f, 0.2f)
	,	m_specular(0.0f, 0.0f, 0.0f)
	,	m_emission(0.0f, 0.0f, 0.0f)
	,	m_textureName("")
	,	m_texture(0)
{
}

Material::~Material()
{
	Cleanup();
}

void Material::Initialize(float shininess, const Color & diffuse, const Color & ambient,
	const Color & specular, const Color & emission, const std::string & textureName)
{
	m_shininess = shininess;
	m_diffuse = diffuse;
	m_ambient = ambient;
	m_specular = specular;
	m_emission = emission;
	m_textureName = textureName;
	LoadTexture();
}

void Material::Cleanup()
{
	m_shininess = 100.0f;
	m_diffuse = Color(0.8f, 0.8f, 0.8f);
	m_ambient = Color(0.2f, 0.2f, 0.2f);
	m_specular = Color(0.0f, 0.0f, 0.0f);
	m_emission = Color(0.0f, 0.0f, 0.0f);
	m_textureName = "";
	FreeTexture();
}

void Material::Set() const
{
	glMaterialfv(GL_FRONT, GL_SHININESS, &m_shininess);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, m_diffuse);
	glMaterialfv(GL_FRONT, GL_AMBIENT, m_ambient);
	glMaterialfv(GL_FRONT, GL_SPECULAR, m_specular);
	glMaterialfv(GL_FRONT, GL_EMISSION, m_emission);
	glBindTexture(GL_TEXTURE_2D, m_texture->Name());
}

void Material::Get()
{
	glGetMaterialfv(GL_FRONT, GL_SHININESS, &m_shininess);
	glGetMaterialfv(GL_FRONT, GL_DIFFUSE, m_diffuse);
	glGetMaterialfv(GL_FRONT, GL_AMBIENT, m_ambient);
	glGetMaterialfv(GL_FRONT, GL_SPECULAR, m_specular);
	glGetMaterialfv(GL_FRONT, GL_EMISSION, m_emission);
}

void Material::LoadTexture()
{
	ImageTGA img;
	img.Load(("artwork\\Textures\\" + m_textureName).c_str());
	m_texture = new Texture(img);
}

void Material::FreeTexture()
{
	delete m_texture;
	m_texture = 0;
}

